pipeline {
    agent any
    stages {

        stage('pull') {
            steps{
                sh "git clone ${params.git_repo} ${params.app_name}/${env.BUILD_NUMBER} && cd ${params.app_name}/${env.BUILD_NUMBER} && git checkout ${params.git_ver}"
            }
        }

        stage('build') {
            steps{
                sh "cd ${params.app_name}/${env.BUILD_NUMBER}  && /var/jenkins_home/${params.maven}/bin/${params.mvn_cmd}"
            }
        }

        stage('package') {
            steps{
                script{
                    sh "cd ${params.app_name}/${env.BUILD_NUMBER}  &&  cd ${params.target_dir} && mkdir project_dir && mv *.jar ./project_dir"
                    checkSame = params.jar_name == params.jar_name.toLowerCase()
                    println "check jar name is same "+ checkSame
                    if(!checkSame){
                       sh "cd ${params.app_name}/${env.BUILD_NUMBER}/${params.target_dir}  && mv ./project_dir/${params.jar_name}-0.0.1-SNAPSHOT.jar  ./project_dir/"+params.jar_name.toLowerCase()+"-0.0.1-SNAPSHOT.jar"
                    }


                }
            }
        }
        stage('image') {
            steps{
                writeFile file: "${params.app_name}/${env.BUILD_NUMBER}/Dockerfile", text: """FROM harbor.hx.com/${params.base_images}
USER root
ADD ${params.target_dir}/project_dir /opt/project_dir
RUN chmod +x /entrypoint.sh"""

                sh "cd ${params.app_name}/${env.BUILD_NUMBER} && docker build -t ${params.image_repo}/${params.image_name}:${params.git_ver}_${params.add_tag} . && docker push ${params.image_repo}/${params.image_name}:${params.git_ver}_${params.add_tag} "
               // sh "cd ${params.app_name}/${env.BUILD_NUMBER} && docker build -t ${params.image_repo}/${params.image_name}:${params.git_ver}_${params.add_tag} . && docker push ${params.image_repo}/${params.image_name}:${params.git_ver}_${params.add_tag} "

            }
        }

        stage('deploy') {
            steps{
                script {
                    def type=params.deploy_type
                    println type
                    println type.indexOf("deployment");
                    if(type.indexOf("deployment")> -1){
                        sh "pwd"
                       // out = sh(script: "ls k8s-deployment.yaml", returnStatus: true)
                        //if (out == 0)
                        out = sh(script: "kubectl  get deployments -n default | awk '{print \$1 }'|grep -Fx ${params.jar_name.toLowerCase()}-deployment |wc -l ", returnStdout:true)
                        println "check deploy file "+out
                        forceUpdate = params.getOrDefault('force_update', false)
                        if (out.toInteger() > 0 && forceUpdate) {
                            sh "kubectl delete -f k8s-deployment.yaml"
                        }
                        sh "sed -e 's#{image_url}#${params.image_repo}/${params.image_name}:${params.git_ver}_${params.add_tag}#g;s#{jar_name}#"+params.jar_name.toLowerCase()+"#g;' common/common.yaml > k8s-deployment.yaml"
                        sh "kubectl  apply -f k8s-deployment.yaml"
                    }

                    if(type.indexOf("service")> -1){
                        //out = sh(script: "ls k8s-service.yaml", returnStatus: true)
                        out = sh(script: "kubectl  get svc -n default | awk '{print \$1 }' |grep -Fx ${params.jar_name.toLowerCase()}-service |wc -l ", returnStdout:true)
                        println "check service file "+out
                        if (out.toInteger() > 0) {
                            sh "kubectl delete -f k8s-service.yaml"
                        }
                        sh "sed -e 's#{jar_name}#"+params.jar_name.toLowerCase()+"#g;' common/common-svc.yaml > k8s-service.yaml"
                        sh "kubectl  apply -f k8s-service.yaml"
                    }

                    if(type.indexOf("ingress")> -1){
                        //out = sh(script: "ls k8s-ingress.yaml", returnStatus: true)
                        out = sh(script: "kubectl  get ingress  -n default| awk '{print \$1 }' |grep -Fx "+params.jar_name.toLowerCase()+"-ingress|wc -l ", returnStdout:true)
                        println "check ingress file "+out
                        if (out.toInteger() > 0) {
                            sh "kubectl delete -f k8s-ingress.yaml"
                        }
                        sh "sed -e 's#{jar_name}#"+params.jar_name.toLowerCase()+"#g;' common/common-ingress.yaml > k8s-ingress.yaml"
                        sh "kubectl  apply -f k8s-ingress.yaml"
                    }

                }


            }
        }
    }
}